package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Override
    public List<SpecParamDTO> querySpecParams(Long categoryId, Long groupId, Boolean searching) {

        //动态sql
        if (categoryId == null && groupId == null){
            // 条件不能都为空
            throw new LyException(400, "查询条件不能为空！");
        }
        //select * from tb_spec_param [where category_id = #{} and group_id = #{} and searching = #{}]
        List<SpecParam> specParamList = this.query()
                .eq(categoryId != null, "category_id", categoryId)
                .eq(groupId != null, "group_id", groupId)
                .eq(searching != null, "searching", searching)
                .list();
        return SpecParamDTO.convertEntityList(specParamList);

    }

    @Override
    public void saveOrUpdateSpecParam(SpecParamDTO specParamDTO) {
        this.saveOrUpdate(specParamDTO.toEntity(SpecParam.class));
    }
}
